package com.example.mywechat;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class LoginActivity extends AppCompatActivity {
    TextView tv_Exit;
    EditText username, password;
    Button btn_login;
    FileOutputStream output = null;
    CheckBox rem;
    String uName="", pwd="";

 /*   protected void onDestroy() {
        super.onDestroy();
        accountDataSave();
    }

    private String loadAccountData() {
        FileInputStream in = null;
        BufferedReader reader;
        StringBuffer content = new StringBuffer();
        try {
            in = openFileInput("accountData1");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "notFound";
        }
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while (true) {
            try {
                if (((line = reader.readLine()) != null))
                    content.append(line);
                else
                    break;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return content.toString();
    }*/

  /*  private void accountDataSave() {
        BufferedWriter writer = null;
        if (rem.isChecked() == true) {
            try {
                output = openFileOutput("accountData", Context.MODE_PRIVATE);
                writer = new BufferedWriter((new OutputStreamWriter(output)));
                writer.write("username" + username.getText().toString() + "\n"
                        + "password" + password.getText().toString());
                System.out.println("保存成功");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }*/

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        tv_Exit = findViewById(R.id.Exit);
        username = findViewById(R.id.username);
        password = findViewById(R.id.password);
        btn_login = findViewById(R.id.login);
        rem = findViewById(R.id.rem);
        tv_Exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               System.exit(0);
            }
        });

        SharedPreferences sp = getSharedPreferences("userInfo", MODE_PRIVATE);
        final SharedPreferences.Editor editor = sp.edit();
        Boolean isRem = sp.getBoolean("isRem", false);
        rem.setChecked(isRem);
        if (isRem == true) {
            username.setText(sp.getString("username", ""));
            password.setText(sp.getString("password", ""));
        }

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uName = username.getText().toString();
                pwd = password.getText().toString();
                if (rem.isChecked() == true) {
                    editor.putBoolean("isRem", true);
                    uName = username.getText().toString();
                    pwd = password.getText().toString();
                    editor.putString("username", uName);
                    editor.putString("password", pwd);
                } else {
                    editor.putBoolean("isRem", false);
                }
                editor.commit();
                if (!(uName.equals("")&&pwd.equals(""))){
                    startActivity(new Intent(getApplicationContext(),MainActivity.class));
                    finish();
                }
            }
        });









        /*        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //if (uName.equals("admin")&&pwd.equals("123456")){
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                if (rem.isChecked() == true) {
                    accountDataSave();
                }
                startActivity(intent);
                //}else {
                //Toast.makeText(getApplicationContext(),"账号或者密码错误!",Toast.LENGTH_SHORT).show();
                // }
            }
        });

        String data = loadAccountData();
        if (!data.equals("notFound")) {
            String saveUname = data.substring(data.indexOf("username") + 8, data.indexOf("password"));
            String savePwd = data.substring(data.indexOf("password") + 8);
            username.setText(saveUname);
            password.setText(savePwd);
        }*/

    }
}
